package com.dayu.web.controller.basic;

import java.math.BigDecimal;
import java.util.List;

import com.dayu.common.config.RuoYiConfig;
import com.dayu.common.utils.file.FileUploadUtils;
import com.dayu.common.utils.file.FileUtils;
import com.dayu.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dayu.common.annotation.Log;
import com.dayu.common.core.controller.BaseController;
import com.dayu.common.core.domain.AjaxResult;
import com.dayu.common.enums.BusinessType;
import com.dayu.basic.domain.BasicFile;
import com.dayu.basic.service.IBasicFileService;
import com.dayu.common.utils.poi.ExcelUtil;
import com.dayu.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 附件管理Controller
 * 
 * @author HK
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/basic/file")
public class BasicFileController extends BaseController
{
    @Autowired
    private IBasicFileService basicFileService;
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询附件管理列表
     */
    @PreAuthorize("@ss.hasPermi('basic:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasicFile basicFile)
    {
        startPage();
        List<BasicFile> list = basicFileService.selectBasicFileList(basicFile);
        return getDataTable(list);
    }

    /**
     * 导出附件管理列表
     */
    @PreAuthorize("@ss.hasPermi('basic:file:export')")
    @Log(title = "附件管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BasicFile basicFile)
    {
        List<BasicFile> list = basicFileService.selectBasicFileList(basicFile);
        ExcelUtil<BasicFile> util = new ExcelUtil<BasicFile>(BasicFile.class);
        return util.exportExcel(list, "file");
    }

    /**
     * 获取附件管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:file:query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId)
    {
        return AjaxResult.success(basicFileService.selectBasicFileById(fileId));
    }

    /**
     * 新增附件管理
     */
    @PreAuthorize("@ss.hasPermi('basic:file:add')")
    @Log(title = "附件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasicFile basicFile)
    {
        return toAjax(basicFileService.insertBasicFile(basicFile));
    }

    /**
     * 修改附件管理
     */
    @PreAuthorize("@ss.hasPermi('basic:file:edit')")
    @Log(title = "附件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasicFile basicFile)
    {
        return toAjax(basicFileService.updateBasicFile(basicFile));
    }

    /**
     * 删除附件管理
     */
    @PreAuthorize("@ss.hasPermi('basic:file:remove')")
    @Log(title = "附件管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds)
    {
        return toAjax(basicFileService.deleteBasicFileByIds(fileIds));
    }

    @Log(title = "文件上传记录", businessType = BusinessType.DELETE)
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file)
    {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        try
        {
            Float size = Float.parseFloat(String.valueOf(file.getSize())) / 1024;
            size = new BigDecimal(size).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + ":8085" + fileName;

            BasicFile basicFile = new BasicFile();
            basicFile.setFileName(fileName);
            basicFile.setFilePath(url);
            basicFile.setFileSize(size.toString());
            basicFile.setFileType(file.getContentType());

            basicFileService.insertBasicFile(basicFile);
            List<BasicFile> list = basicFileService.selectBasicFileList(basicFile);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("file", list.get(0));
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}
